<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>登录|注册</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div id="main_div" class="container">
    <ul class="nav nav-tabs justify-content-center" role="tablist">
        <li class="nav-item">
            <a id="login_tag" class="nav-link active" data-toggle="tab" href="#login">登录</a>
        </li>
        <li class="nav-item">
            <a class="nav-link" data-toggle="tab" href="#register">注册</a>
        </li>
    </ul>

    <div class="tab-content">
        <div id="login" class="container tab-pane active"><br>
            <form id="loginForm" method="post">
                <div class="card" >
                    <div class="card-header"><small id="login-info" class="text-danger"> </small></div>
                    <div class="card-body">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text">账号</span>
                            </div>
                            <input id="login_id" name="id" type="text" class="form-control" placeholder="账号" required="required">
                        </div>
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text">密码</span>
                            </div>
                            <input id="login_pwd" name="password" type="password" class="form-control" placeholder="密码"
                                   required="required">
                        </div>
                        <div class="radio">
                            &nbsp;&nbsp;&nbsp;<label><input type="radio" name="type" value="0" checked="checked">用户</label>&nbsp;&nbsp;&nbsp;
                            <label><input type="radio" name="type" value="1">管理员</label>
                        </div>
                    </div>
                    <div class="card-footer">
                        <input type="button" onclick="login()" class="btn btn-outline-primary justify-content-center" value="登录">
                    </div>
                </div>
            </form>
        </div>
        <div id="register" class="container tab-pane fade"><br>
            <form id="registerForm" method="post">
                <div class="card" >
                    <div class="card-header"><small id="register-info" class="text-danger"> </small></div>
                    <div class="card-body">
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text">账号</span>
                            </div>
                            <input id="register_id" name="id" type="text" class="form-control" placeholder="账号" required="required">
                        </div>
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text">名字</span>
                            </div>
                            <input id="register_name" name="name" type="text" class="form-control" placeholder="名字" required="required">
                        </div>
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                                <span class="input-group-text">密码</span>
                            </div>
                            <input id="register_pwd" name="password" type="password" class="form-control" placeholder="密码"
                                   required="required">
                        </div>
                    </div>
                    <div class="card-footer">
                        <input type="button" onclick="register()" class="btn btn-outline-primary justify-content-center" value="注册">
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var content_path = "${pageContext.request.contextPath}";
    var flag;
    if(window.parent == this.window) {  // 不存在存在父窗口<div class="card" style="width: 50%;margin: 0 auto;">
        $("#main_div").prepend("<h3>无操作权限请先登录！ </h3><br>");
        $(".card").css({"width": "50%","margin": "0 auto"});
        flag=true;
    }
    function login() {
        $.ajax({
            url: "${pageContext.request.contextPath}/user/login",
            type: 'post',
            data: {"id":$("#login_id").val(), "password":$("#login_pwd").val(), "type":$("input[name='type']:checked").val()},
            dataType:'json',
            success: function (data) {
                if (data["result"] == 1) {
                    if(flag){  // 不存在存在父窗口
                        window.location= content_path;
                    }else {
                        setTimeout(function () {
                            parent.location.reload();
                        }, 100);
                    }
                }
                $("#login-info").text(data['info']);
                setTimeout(function () {
                    $("#login-info").text("");
                }, 3000);
            }
        });
    }

    function register() {
        $.ajax({
            url: "${pageContext.request.contextPath}/user/register",
            type: 'POST',
            data: {"id":$("#register_id").val(),"name":$("#register_name").val(), "password":$("#register_pwd").val()},
            dataType:'json',
            success: function (data) {
                setTimeout(function () {
                    $("#register-info").text("");
                }, 3000);
                if (data["result"] == 1) {
                    $("#login_tag").click();
                    $("#login-info").text(data['info']);
                }else {
                    $("#register-info").text(data['info']);
                }
            }
        });
    }
</script>
</body>
</html>